package net.sasuke.firstapp;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyContextListener implements ServletContextListener {

   @Override
   public void contextDestroyed(ServletContextEvent event) {
      ServletContext ctx = event.getServletContext();
      System.out.println("Destroying DB connections for DB: " + ctx.getAttribute("DATABASE"));
      File f = new File("C:/TMP.txt");
      f.delete();
   }

   @Override
   public void contextInitialized(ServletContextEvent event) {
      Object o = event.getSource();
      System.out.println("Source: " + o);
      ServletContext ctx = event.getServletContext();
      ctx.setAttribute("DATABASE", "MyDB");
      System.out.println("Creating DB connections for DB: " + ctx.getAttribute("DATABASE"));
      File f = new File("C:/TMP.txt");
      try {
         f.createNewFile();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

}
